/*
 * @Author: 肖燕 2461435122@qq.com
 * @Date: 2024-06-06 09:35:59
 * @LastEditors: 肖燕 2461435122@qq.com
 * @LastEditTime: 2024-10-30 10:55:30
 * @FilePath: /mrismodulecash/src/assets/scripts/cashlink.js
 * @Description: 综合分析时间格式
 */

// 结余分析时间get和set
import store from '@/assets/scripts/store'
import { isWsDoctor } from '@/router/getws-router'
import { formatDateSelect } from '@/assets/scripts/tools/utils.js'
import moment from 'moment'

export function initCashDate (type) {
  if (type === 'month') {
    return {
      type: type,
      value: new Date(moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD'))
    }
  }
  return {
    type: type || 'monthrange',
    value: [
      new Date(moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD')),
      new Date(moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD'))
    ]
  }
}

export function getDateShow () {
  const date = JSON.parse(JSON.stringify(getCashLink().dateSelect || {}))
  if (date.type === 'year') {
    return moment(date.value).format('YYYY')
  } else if (date.type === 'month') {
    return moment(date.value).format('YYYY-MM')
  } else if (date.type === 'quarter') {
    return moment(date.value).format('YYYY') + '年第' + date.quarter + '季度'
  } else if (date.type === 'monthrange') {
    return moment(date.value[0]).format('YYYY-MM') + '~' + moment(date.value[1]).format('YYYY-MM')
  } else if (date.type === 'datetimerange') {
    return moment(date.value[0]).format('YYYY-MM-DD HH:mm:ss') + '~' + moment(date.value[1]).format('YYYY-MM-DD HH:mm:ss')
  }
  return ''
}

export function getDateOpt (dateSelect) {
  return Object.assign({}, formatDateSelect(dateSelect) || {}, { type: dateSelect.type })
}

export function getCashDate (dateSelect) {
  if (isWsDoctor()) {
    return dateSelect || initCashDate()
  }
  return store.state.storeCashLink.dateSelect || dateSelect || initCashDate()
}

export function setCashDate (dateSelect) {
  setCashLink(JSON.parse(JSON.stringify(Object.assign({}, store.state.storeCashLink, dateSelect))))
}

export function setCashLink (data) {
  store.commit('SET_CASHLINK', data)
}

export function getCashLink () {
  return store.state.storeCashLink
}
